const mongoose = require('mongoose');
const Account = require('./models/account')
const MNStatusModel = require('./models/mnstatushistory')

var db = mongoose.connection.openUri("mongodb://test:Abcd1234@39.107.123.138:27017/cb");
// var account = Account({
//     accountName: "chao-pc"
// });

// account.save(function (err, doc) {
//     if (err) {
//         console.log('save error:' + err);
//     }

//     console.log('save sucess \n' + doc);
// })

async function test() {
    var account = Account({
        accountName: "chao-pc"
    });
    console.log("test begin...")
    await account.save()
    console.log("saved.")
}

async function getAllAccount(){
    accounts = await Account.find()
}

async function insertMNStatus(coinSymbol, activatedMN, roi, price, updatedDate){
    var status = MNStatusModel({
        CoinSymbol: coinSymbol,
        ActivatedMasterNode: Number.parseInt(activatedMN),
        ROI : roi,
        Price : price,
        Updated : updatedDate
    })

    status.save()
}

async function queryLastItemByDateDesc(symbol){
    return await MNStatusModel.find({"CoinSymbol":symbol}).sort({Updated:-1})
}

getAllAccount();

module.exports = {
    "test":test,
    "insertMNStatus":insertMNStatus,
    "queryLastItemByDateDesc" : queryLastItemByDateDesc
}